package com.example.jason.helloworld;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
    public static String mMailNum;
    public static String mMailPsw;
    public static boolean mSmsListenEnabled = false;
    public static boolean mCallListenEnabled = false;
    public static MyHandler mMyHandle;
    public static SimpleMailSender mMailSender;
    public EditText mNumTxt;
    public EditText mPswTxt;
    public Button mBtnTest;
    public Button mBtnSms;
    public Button mBtnCall;
    public TextView mRecTxt;
    public TextView mSendRstTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMyHandle = new MyHandler();
        mMailSender = new SimpleMailSender();

        mNumTxt = findViewById(R.id.mailNumTxt);
        mPswTxt = findViewById(R.id.mailPswTxt);
        mBtnTest = findViewById(R.id.btnTest);
        mBtnSms = findViewById(R.id.btnSms);
        mBtnCall = findViewById(R.id.btnCall);
        mRecTxt = findViewById(R.id.recTxt);
        mSendRstTxt = findViewById(R.id.sendRstTxt);

        // TextView垂直滚动条
        mRecTxt.setMovementMethod(ScrollingMovementMethod.getInstance());

//        // 限制邮箱账号输入
//        mNumTxt.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);

        // 读取账号密码
        FileUtil.readFromPre(MainActivity.this, mNumTxt, mPswTxt);

        // 横屏竖屏转换重绘时
        if (mSmsListenEnabled) {
            mBtnSms.setText(R.string.btnSmsStop);
        } else {
            mBtnSms.setText(R.string.btnSms);
        }
        if (mCallListenEnabled) {
            mBtnCall.setText(R.string.btnCallStop);
        } else {
            mBtnCall.setText(R.string.btnCall);
        }
        if(mSmsListenEnabled || mCallListenEnabled)
        {
            mNumTxt.setEnabled(false);
            mPswTxt.setEnabled(false);
        }

        mBtnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mMailNum = mNumTxt.getText().toString();
                mMailPsw = mPswTxt.getText().toString();
                if (mMailNum.isEmpty() || mMailPsw.isEmpty()) {
                    Toast.makeText(MainActivity.this, "账号或密码不能为空！", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "发送测试邮件！", Toast.LENGTH_SHORT).show();
                    // 输入框不可编辑
                    mNumTxt.setEnabled(false);
                    mPswTxt.setEnabled(false);
                    mBtnTest.setEnabled(false);
                    // 验证登陆
                    final String sms = "今天又是美好的一天，加油努力干！";
                    mRecTxt.setText(sms);
                    mMailSender.sendEmail("HelloWorld", sms, mMailNum, mMailPsw);
                }
            }
        });

        mBtnSms.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListenButtonDispose(mBtnSms);
            }
        });

        mBtnCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ListenButtonDispose(mBtnCall);
            }
        });
    }

    private void ListenButtonDispose(Button btn) {
        String str;

        switch (btn.getId()) {
            case R.id.btnSms:
                mSmsListenEnabled = !mSmsListenEnabled;
                if (mSmsListenEnabled) {
                    str = "正在监听新短信！";
                    mBtnSms.setText(R.string.btnSmsStop);
                } else {
                    str = "停止监听新短信！";
                    mBtnSms.setText(R.string.btnSms);
                }
                break;
            case R.id.btnCall:
                mCallListenEnabled = !mCallListenEnabled;
                if (mCallListenEnabled) {
                    str = "正在监听新来电！";
                    mBtnCall.setText(R.string.btnCallStop);
                } else {
                    str = "停止监听新来电！";
                    mBtnCall.setText(R.string.btnCall);
                }
                break;
            default:
                str = "<ERROR>";
                break;
        }

        // 设置输入框编辑状态
        mNumTxt.setEnabled(!mSmsListenEnabled && !mCallListenEnabled);
        mPswTxt.setEnabled(!mSmsListenEnabled && !mCallListenEnabled);

        Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
    }


    class MyHandler extends Handler {
        public MyHandler() {
        }

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);

            Calendar cld = Calendar.getInstance();
            String now = "[" + cld.get(Calendar.YEAR) + "." + (cld.get(Calendar.MONTH)+1) + "." + cld.get(Calendar.DAY_OF_MONTH)
                    + " " + cld.get(Calendar.HOUR_OF_DAY) + ":" + cld.get(Calendar.MINUTE) + ":" + cld.get(Calendar.SECOND) + "]";

            String str;
            Boolean isSuccess;
            String type = msg.getData().getString("type");
            if (type != null) {
                switch (type) {
                    case "sms":
                        str = msg.getData().getString(type);
                        mRecTxt.setText(("SMS" + now + "\r\n" + str));
                        mMailSender.sendEmail("SMS" + now, str, mMailNum, mMailPsw);
                        break;
                    case "call":
                        str = msg.getData().getString(type);
                        mRecTxt.setText(("CALL" + now + "\r\n" + str));
                        mMailSender.sendEmail("CALL" + now, str, mMailNum, mMailPsw);
                        break;
                    case "send":
                        isSuccess = msg.getData().getBoolean(type);
                        if (isSuccess) {
                            str = "发送成功！";
                            if (!mSmsListenEnabled && !mCallListenEnabled) {
                                // 成功则提示并保存账号密码
                                FileUtil.saveToPre(MainActivity.this,
                                        mNumTxt.getText().toString(), mPswTxt.getText().toString());
                            }
                        } else {
                            str = "发送失败！";
                            if (!mSmsListenEnabled && !mCallListenEnabled) {
                                // TODO：检查网络
                                // TODO：重试几次
                            }
                        }
                        Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                        mSendRstTxt.setText((str + now));

                        // 如果测试邮件发送成功后使能按钮和输入框
                        mBtnTest.setEnabled(true);
                        if (!mSmsListenEnabled && !mCallListenEnabled) {
                            mNumTxt.setEnabled(true);
                            mPswTxt.setEnabled(true);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
    }
}


